<?php

class APP_User_Profile extends APP_View_Page {
	private $error;

	function __construct() {
		parent::__construct( 'edit-profile.php', __( 'Edit Profile', APP_TD ) );
		add_action( 'init', array( $this, 'update') );
	}

	static function get_id() {
		return parent::_get_id( __CLASS__ );
	}

	function update() {
            if (!isset($_POST['action']) || 'app-edit-profile' != $_POST['action'])
                return;
            check_admin_referer('app-edit-profile');
            require ABSPATH . '/wp-admin/includes/user.php';
            //update custom field here

            $user_id = $_POST['user_id'];
            if(isset($_POST['user_type'])){
            	if ($_POST['user_type'] == 1) {//nomal user
            		//remove xss here
            	 	update_usermeta( $user_id, 'user_type', $_POST['user_type'] );
            		update_usermeta( $user_id, 'birthday', $_POST['birthday'] );
	                update_usermeta( $user_id, 'phone', $_POST['phone'] );
        	        update_usermeta( $user_id, 'address', $_POST['address'] );
    	          	update_usermeta( $user_id, 'description', $_POST['description'] );
                    update_usermeta( $user_id, 'sex', $_POST['sex'] );
            	}

            	if ($_POST['user_type'] == 2) {//shop user
            		//remove xss here
            	 	update_usermeta( $user_id, 'user_type', $_POST['user_type'] );
            		update_usermeta( $user_id, 'person_boss', $_POST['person_boss'] );
	                update_usermeta( $user_id, 'start_day', $_POST['start_day'] );
        	        update_usermeta( $user_id, 'hotline', $_POST['hotline'] );
    	          	update_usermeta( $user_id, 'website_link', $_POST['website_link'] );
    	          	update_usermeta( $user_id, 'yahoo_nick', $_POST['yahoo_nick'] );
					update_usermeta( $user_id, 'skype_nick', $_POST['skype_nick'] );
					update_usermeta( $user_id, 'facebook_url', $_POST['facebook_url'] );
    	          	update_usermeta( $user_id, 'slogan', $_POST['slogan'] );
    	          	update_usermeta( $user_id, 'description_shop', $_POST['description_shop'] );
            	}
                
            }

            $r = edit_user($_POST['user_id']);
		    if (is_wp_error($r)) {
                $this->error = $r->get_error_message();
            } else {
                $redirect_url = add_query_arg(array('updated' => 'true'));
                wp_redirect($redirect_url);
                exit();
            }
        }

	function template_redirect() {
		// Prevent non-logged-in users from accessing the edit-profile.php page
		appthemes_auth_redirect_login();

		add_action( 'appthemes_notices', array( $this, 'show_notice' ) );
	}

	function show_notice() {
		if ( !empty( $this->error ) ) {
			appthemes_display_notice( 'error', $this->error );
		} elseif ( isset( $_GET['updated'] ) ) {
			appthemes_display_notice( 'success', __( 'Cập nhật thông tin thành công.', APP_TD ) );
		}
	}
}

function appthemes_get_edit_profile_url() {
    	if ( $page_id = APP_User_Profile::get_id() )
		return get_permalink( $page_id );

	return get_edit_profile_url( get_current_user_id() );
}

